/*
 * MENU.c
 *
 * Created: 24.09.2012 17:55:40
 *  Author: mariuvi
 */ 
#include "MENU.h"
#include "DISPLAY.h"
#include "JOY.h"
#include "definitions.h"
#include <avr/io.h>

static unsigned int browse = FALSE;
int cnt = 0;

typedef const struct MenuStucture
{
	const char* text;
	int numMenupoints;
	int up;
	int down;
	int enter;
	MENU_status status;
} MenuEntry;


unsigned char MENU_selected = 0;
unsigned char MENU_selected_prev = 0;

const char menu_000[] = "Ping pong";
const char menu_001[] = "Options";
const char menu_002[] = "SRAM test";
const char menu_003[] = "Options";
const char menu_004[] = "Options2";
const char menu_005[] = "Options3";

const char menu_100[] = "Play";
const char menu_101[] = "Track";
const char menu_102[] = "Return";

const char menu_200[] = "Change PD values";
const char menu_201[] = "Return";
const char menu_202[] = "";


MenuEntry MENU_menu[] =
{
	{menu_000, 6, 0, 1, 6, BROWSE}, //0
	{menu_001, 6, 0, 2, 9, BROWSE}, //1
	{menu_002, 6, 1, 3, 2, SRAM_TEST}, //2	 
	{menu_003, 6, 2, 4, 3, BROWSE}, //3
	{menu_004, 6, 3, 5, 4, BROWSE}, //4
	{menu_005, 6, 4, 5, 5, BROWSE}, //5
		
	{menu_100, 3, 6, 7, 6, PLAY}, //6
	{menu_101, 3, 6, 8, 7, TRACK}, //7
	{menu_102, 3, 7, 8, 0, BROWSE},  //8
		
	{menu_200, 3, 9, 10, 11, CHANGE_PD}, //9
	{menu_201, 3, 9, 10, 0, BROWSE},  //10
	{menu_202, 3, 9, 10, 0, BROWSE},  //11
};

void MENU_init()
{
	//Sets up a timer with a 1/64 clock
	BIT_SET(TCCR1B,CS30);
	BIT_SET(TCCR1B,CS31);
	BIT_CLEAR(TCCR1B,CS32);
	
	MENU_selected = MENU_selected_prev;
	JOY_button_init();
	DISPLAY_init();

	//DISPLAY_border('#');
	MENU_print_menu();
		
	TCNT1 = 0;
	browse = FALSE;	
}

void MENU_print_menu()
{
	int till = 0;
	int from;
	
	//Calculates till and from
	while(till<=MENU_selected)
	{
		till += MENU_menu[till].numMenupoints;
	}
	from = till - MENU_menu[MENU_selected].numMenupoints;
	till -= 1;
	
	//Marker formatting in menu
	//Top of menu
	if(MENU_selected == from)
	{
		DISPLAY_move_marker(UP);
		DISPLAY_move_menu(MENU_menu[from].text, MENU_menu[from+1].text, MENU_menu[from+2].text);
	}
	else if(MENU_selected == till)
	{
		DISPLAY_move_marker(DOWN);
		DISPLAY_move_menu(MENU_menu[till-2].text, MENU_menu[till -1].text, MENU_menu[till].text);
	}
	else 
	{
		DISPLAY_move_marker(NEUTRAL);
		DISPLAY_move_menu(MENU_menu[MENU_selected-1].text, MENU_menu[MENU_selected].text, MENU_menu[MENU_selected+1].text);
	}
}

void MENU_browse()
{
	if((TCNT1 > 62500) && (browse == FALSE))
	{
		browse = TRUE;
	}
	
	//We only browse once every 0.5s
	if(browse == TRUE)
	{		
							
		JOY_Pos position = JOY_read();
		if(JOY_button_pressed())
		{
			
			TCNT1 = 0;
			browse = FALSE;
			USB_state = MENU_menu[MENU_selected].status;
			
			MENU_selected_prev = MENU_selected;
			MENU_selected = MENU_menu[MENU_selected].enter;
			MENU_print_menu();
		}
		else if(position.dir == UP)
		{
			MENU_selected = MENU_menu[MENU_selected].up;
			TCNT1 = 0;
			browse = FALSE;
			MENU_print_menu();
		}
		else if(position.dir == DOWN)
		{
			MENU_selected = MENU_menu[MENU_selected].down;
			TCNT1 = 0;
			browse = FALSE;
			MENU_print_menu();
		}			
	}
}
